package com.hoo.controller.account;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.hoo.auth.FormAuthenticationFilter;
import com.hoo.common.constant.Constant;
import com.hoo.common.constant.Result;
import com.hoo.entity.account.Account;
import com.hoo.entity.account.Users;
import com.hoo.form.account.UsersForm;
import com.hoo.service.account.AccountService;

@Controller
public class LoginController extends BaseController {

	@Autowired
	AccountService accountService;

	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public String success() {
		return "account/success";
	}

	@RequestMapping(value = "/sign_in", method = RequestMethod.GET)
	public String signIn(@ModelAttribute("form") UsersForm form,BindingResult result, HttpServletRequest request, Model model) {
		if (getUserContext() != null && getCurrentUserId() != null) {
			return "redirect:/account/user_info";
		}
		String error = (String) request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
		if (error != null) {
			Result result1 = new Result();
			Errors errors = new BeanPropertyBindingResult(new UsersForm(), "form");
			if ("org.apache.shiro.authc.IncorrectCredentialsException".equals(error)) {
				errors.rejectValue("loginPassword", "", "密码错误");
			} else if ("com.hoo.exception.UnknownAccountException".equals(error)) {
				errors.rejectValue("loginName", "", "账号不存在");
			} else if ("com.hoo.exception.LockedAccountException".equals(error)) {
				errors.rejectValue("loginName", "", "账号被锁定");
			}else if ("com.hoo.exception.CaptchaWrongException".equals(error)) {
				errors.rejectValue("captcha", "", "验证码错误");
			} else {
				errors.rejectValue("loginPassword", "", "未知错误");
			}
			result1.setErrors(errors);
		    result.addAllErrors(result1.getErrors());
		}
		return "account/signIn";
	}

	@RequestMapping(value = "/sign_in", method = RequestMethod.POST)
	public String doSignIn(@Valid @ModelAttribute("form") UsersForm form,
			BindingResult result, RedirectAttributes redirectAttributes,
			HttpServletRequest request, Model model) {
		if (getUserContext() != null && getCurrentUserId() != null) {
			redirectAttributes.addFlashAttribute(Constant.OK_MSG, "登录成功");
			redirectAttributes.addFlashAttribute("realName", getUserContext()
					.getRealName());
			return "redirect:/account/user_info";
		}
		return signIn(form, result, request,model);
	}

	@RequestMapping(value = "/sign_up", method = RequestMethod.GET)
	public String signUp(@ModelAttribute("form") Users user,
			BindingResult result, HttpServletRequest request,
			HttpServletResponse response) {
		return "account/addUser";
	}

	/*@RequestMapping(value = "/sign_up", method = RequestMethod.POST)
	public String doSignUp(@Valid @ModelAttribute("form") Users user,
			BindingResult result, RedirectAttributes redirectAttributes,
			HttpServletRequest request, HttpServletResponse response) {
		if (result.hasErrors()) {
			return signUp(user, result, request, response);
		}
		user.setReferer(request.getHeader("referer") == null ? "" : StringUtils
				.substring(request.getHeader("referer"), 0, 200));
		Result res = accountService.saveUser(user);
		// 生成用户失败将错误返回到页面
		Errors errors = res.getErrors();
		if (errors != null && errors.hasErrors()) {
			result.addAllErrors(errors);
			return signUp(user, result, request, response);
		}
		redirectAttributes.addFlashAttribute(Constant.OK_MSG, "注册成功，请登录");
		return "redirect:/sign_in";
	}
*/
	@RequestMapping(value = "/sign_out", method = RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		SecurityUtils.getSubject().logout();
		request.getSession().invalidate();
		return "redirect:/index";
	}
}
